package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.comment.domain.Wenda;
import cn.wolfcode.luowowo.comment.domain.WendaComment;
import cn.wolfcode.luowowo.comment.service.IWendaCommentService;
import cn.wolfcode.luowowo.comment.service.IWendaService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.LoginUser;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import cn.wolfcode.luowowo.website.util.UMEditorUploader;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.UUID;

@Controller
@RequestMapping("/wenda")
public class WendaController {

    @Reference
    private IWendaService wendaService;
    @Reference
    private IDestinationService destinationService;
    @Value("${file.dir}")
    private String dir;
    @Reference
    private IWendaCommentService wendaCommentService;
    @Reference
    private IUserInfoService userInfoService;

    @RequestMapping("")
    public String wendaIndex(Model model){
        //共享所有的社区提问
        List<Wenda> o = wendaService.listAll();
        model.addAttribute("wendas", o);
        return "wenda/wenda";
    }

    @RequestMapping("public")
    @RequiredLogin
    public String input(Model model){
        //dests
        model.addAttribute("dests",destinationService.list());
        return "wenda/public";
    }

    @RequestMapping("wendaThumbup")
    @ResponseBody
    public AjaxResult wendaThumbup(String wendaId,@LoginUser UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        if (userInfo == null) {
            result.setCode(102);
            result.mark("请先登录");//表示没有登录的错误状态码
            return result;
        }
        //点赞数增加
        Wenda wenda = wendaService.getWenda(wendaId);
        Integer thumbupnum = wenda.getThumbupnum();
        wenda.setThumbupnum(thumbupnum + 1);

        List<Long> thumbups = wenda.getThumbups();
        if (thumbups.contains(userInfo.getId())) {
            result.setSuccess(false);
        }
        if (!thumbups.contains(userInfo.getId())) {
            thumbups.add(userInfo.getId());
            //点赞成功
            result = wendaService.save(wenda);
        }
        return result.addData(wenda);
    }

    @RequestMapping("ranking")
    public String rangking(int rankingId,Model model){
        List<Long> userIds = wendaService.listTop10(rankingId);
        List<UserInfo> userList = userInfoService.listByUserIds(userIds);
        if (userList.size() > 10) {
            userList = userList.subList(0,10);
        }
        model.addAttribute("wendas", userList);
        return "wenda/wendaRanking";
    }

    @RequestMapping("save")
    @ResponseBody
    public AjaxResult save(Wenda wenda, @LoginUser UserInfo userInfo){
        Destination dest = destinationService.get(wenda.getDestId());
        wenda.setCoverUrl(dest.getCoverUrl());
        wenda.setDestName(dest.getName());
        wenda.setUserId(userInfo.getId());
        wenda.setUsername(userInfo.getNickname());
        wenda.setHeadImgUrl(userInfo.getHeadImgUrl());
        wenda.setLevel(userInfo.getLevel());
        return wendaService.save(wenda);
    }



    //上传图片
    @RequestMapping("/contentImage")
    @ResponseBody
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile, dir);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }
}
